package com.zeedev.flyingsaucer.beers.activities.login;

import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.zeedev.flyingsaucer.beers.activities.storechooser.StoreSpinner;
import com.zeedev.flyingsaucer.global.UfoClubApp;
import com.zeedev.flyingsaucer.ufoclub.R;
import mobi.zeedev.flyingsaucer.common.CredentialsModule.interfaces.IPreferenceCredentials;
import mobi.zeedev.flyingsaucer.common.WebLinkModule.interfaces.UfoClubClient;
import roboguice.inject.ContextScoped;
import roboguice.inject.InjectView;

/**
 * User: James Johnson
 * Date: Oct 28, 2010
 * Time: 4:20:56 PM
 */
@ContextScoped
public class LoginController implements ILoginController {

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // View Injections
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    @InjectView(R.id.passwordEditText)
    EditText passwordEditText;

    @InjectView(R.id.rememberMeCheckBox)
    CheckBox rememberCheckBox;

    @InjectView(R.id.cardNumberEditText)
    EditText cardNumberEditText;

    @InjectView(R.id.storeNumberSpinner2)
    StoreSpinner storeSpinner;

    @InjectView(R.id.checkbox_autologon)
    CheckBox checkAutoLogon;

    @InjectView(R.id.checkbox_mou_member)
    CheckBox checkMouMember;

    @InjectView(R.id.loginButton)
    Button loginButton;

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Injections
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    @Inject
    UfoClubApp app;

    @Inject
    Activity mActivity;

    @Inject
    @Named("autoIPreferenceCredentials")
    IPreferenceCredentials credentials;

//    @Inject
//    IBeerWebQuery webQuery;

    @Inject
    UfoClubClient ufoClubClient;

    @Inject
    public LoginController() {

        //init

        //check if stores exist (automatic) (StoreSpinner)
        //if stores over 6 months old, refresh (automatic) (StoreSpinner)
        //otherwise load store list  (automatic)

        //load credentials from preferences
        //this is also automatic because there is only one default preferences for credentials now


    }

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Functions / Business Logic
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    @Override
    public void resetCredentials() {
        credentials.getCredentials().reset();
        credentials.bindToPreferences();
//        resetBeerRefreshDate();
    }

//    public void resetBeerRefreshDate() {
//        app.setLastBeerLookup(null);
//    }


    @Override
    public void saveCredentials() {
        credentials.bindToPreferences();
//        webQuery.injectCookies(credentials.getCredentials().generateCookieInfo());
//        ufoClubClient.
    }

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Other
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    public void setRememberMe(Boolean isRemember) {
        credentials.getCredentials().setUfoRemember(isRemember);
        if (!isRemember) {
            resetCredentials();
        }
    }

    public void setAsNonMember(Boolean isNonMember) {
        if (isNonMember) {
            passwordEditText.setVisibility(View.GONE);
            cardNumberEditText.setVisibility(View.GONE);
            mActivity.findViewById(R.id.cardNumberLabel).setVisibility(View.GONE);
            mActivity.findViewById(R.id.passwordLabel).setVisibility(View.GONE);
            mActivity.findViewById(R.id.login_member_login_text).setVisibility(View.GONE);
        } else {
            passwordEditText.setVisibility(View.VISIBLE);
            cardNumberEditText.setVisibility(View.VISIBLE);
            mActivity.findViewById(R.id.cardNumberLabel).setVisibility(View.VISIBLE);
            mActivity.findViewById(R.id.passwordLabel).setVisibility(View.VISIBLE);
            mActivity.findViewById(R.id.login_member_login_text).setVisibility(View.VISIBLE);
        }
    }



}
